<template>

  <el-dialog
    :title="title"
    :visible.sync="show"
    width="30%"
    @close="close"
  >
    <el-form ref="form" :model="dept" :rules="rules" class="form" label-position="right" label-width="80px">
      <el-form-item label="上级部门" prop="parentId">
        <select-tree ref="selectTree" :parent-id.sync="dept.parentId" :tree="tree" />
      </el-form-item>
      <el-form-item label="部门编号" prop="deptCode">
        <el-input v-model="dept.deptCode" />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="dept.deptName" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dept.sort" controls-position="right" />
      </el-form-item>
      <el-form-item label="部门状态" prop="state">
        <el-radio v-model="dept.state" :label="false">否</el-radio>
        <el-radio v-model="dept.state" :label="true">是</el-radio>
      </el-form-item>
      <el-row class="form-button">
        <el-button type="primary" @click="save">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { find, save, update, tree } from '@/api/sys/dept'
import svgIcons from '@/views/icons/svg-icons'
import SelectTree from '@/components/SelectTree/index'

export default {
  name: 'DeptForm',
  components: { SelectTree },
  props: {
    id: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      default: '添加字典'
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dept: { parentId: null, deptCode: '', deptName: '', state: true, sort: 0 },
      options: [{ label: '停用', value: 0 }, { label: '正常', value: 1 }],
      rules: {
        deptCode: [
          { required: true, message: '请输入部门编号', trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序值', trigger: 'blur' }
        ]
      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      svgIcons,
      tree: []
    }
  },
  watch: {
    id(val) {
      if (val) { this.find() }
    }
  },
  created() {
    this.findTree()
  },
  methods: {
    close() {
      this.$refs['form'].resetFields()
      this.dept = { parentId: null, deptCode: '', deptName: '', state: true, sort: 0 }
      this.$refs.selectTree.$refs.tree.setCurrentKey(null)
      this.$emit('update:show', false)
      this.$emit('update:id', null)
    },
    save() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          if (this.id) {
            await update(this.dept)
            this.responseRes()
          } else {
            await save(this.dept)
            this.responseRes()
          }
        }
      })
    },
    async find() {
      const { data } = await find(this.id)
      this.dept = data
    },

    responseRes() {
      this.close()
      this.$parent.findPage()
    },
    async findTree() {
      const { data } = await tree()
      this.tree = data
    }
  }
}
</script>

<style scoped>
</style>
